package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.domain.vo.ProductSpecVo;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "商品规格接口管理")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {

    @Autowired
    ProductSpecService productSpecService;



    @Operation(summary = "根据三级分类id查询分类下的所有规格值列表")
    @GetMapping("/list/{c3Id}")
    public AjaxResult listByC3id(@PathVariable("c3id")Long c3id){

      return success( productSpecService.list(new LambdaQueryWrapper<ProductSpec>().eq(ProductSpec::getCategoryId,c3id)));

    }

    @Operation(summary = "查询商品规格列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec){
            startPage();
          List<ProductSpecVo> list =productSpecService.selectProductSpecList(productSpec);
          return getDataTable(list);
    }

/**
 * 获取商品规格详细信息
 */
    @Operation(summary = "获取商品规格详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productSpecService.selectProductSpecById(id));
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    public AjaxResult addProductSpec(@Validated @RequestBody ProductSpec productSpec){
          return  success(productSpecService.save(productSpec));
    }

    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult updateProductSpec(@Validated @RequestBody ProductSpec productSpec){
        productSpec.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("{ids}")
    public AjaxResult deleteProductSpec(@PathVariable("ids") Long[] ids){
        return toAjax(productSpecService.removeBatchByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "根据分类id获取商品规格列表")
    @GetMapping("/getSpecList/{categoryId}")
    public AjaxResult list(@PathVariable("categoryId")Long categoryId){
        return success(productSpecService.getSpecList(categoryId));
    }


}
